package cn.e4j.config.properties;

import cn.e4j.core.util.SpringContextHolder;
import cn.e4j.core.util.ToolUtil;
import com.baomidou.dynamic.datasource.toolkit.CryptoUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.io.File;

import static cn.e4j.core.util.ToolUtil.getTempPath;

/**
 * 项目配置
 *
 * @author wenchuan.li
 * @Date 2018/5/23 22:31
 */
@Component
@ConfigurationProperties(prefix = "e4j")
public class E4jProperties {

    private Boolean kaptchaOpen = false;

    private Boolean swaggerOpen = false;

    private String fileUploadPath;

    private int userCount;

    private Boolean haveCreatePath = false;

    private String erpVersion;

    private String ediUploadPath;

    private Boolean haveCreateEdiPath = false;

    private Boolean springSessionOpen = false;

    /**
     * session 失效时间（默认为20个小时 单位：秒）
     */
    private Integer sessionInvalidateTime = 20 * 60 * 60;//4*60 * 60

    /**
     * session 验证失效时间（默认为10分钟 单位：秒）
     */
    private Integer sessionValidationInterval = 10 * 60;

    private String cachePrefix = "e4j_";

    private String erp;

    private String dbType;


    public String getErp() {
        return erp;
    }

    public void setErp(String erp) {
        this.erp = erp;
    }

    public String getDbType() {
        return dbType;
    }

    public void setDbType(String dbType) {
        this.dbType = dbType;
    }

    public String getCachePrefix() {
        return cachePrefix;
    }

    public void setCachePrefix(String cachePrefix) {
        this.cachePrefix = cachePrefix;
    }

    public String getFileUploadPath() {
        //判断目录存不存在,不存在得加上
        if (ToolUtil.isEmpty(fileUploadPath)) {
            return getTempPath();
        } else {
            //判断有没有结尾符,没有得加上
            if (!fileUploadPath.endsWith(File.separator)) {
                fileUploadPath = fileUploadPath + File.separator;
            }
            //判断目录存不存在,不存在得加上
            if (!haveCreatePath) {
                File file = new File(fileUploadPath);
                file.mkdirs();
                haveCreatePath = true;
            }
            return fileUploadPath;
        }
    }

    public void setFileUploadPath(String fileUploadPath) {
        this.fileUploadPath = fileUploadPath;
    }

    public int getUserCount() {
        if(this.userCount == 0){
            this.userCount = 30;
        }
        return userCount;
    }

    public void setUserCount(String userCount) {
        try{
            this.userCount = Integer.valueOf(CryptoUtils.decrypt(userCount));
        }catch (Exception e){
            this.userCount = 30;
        }
    }

    public String getErpVersion() {
        return erpVersion;
    }

    public void setErpVersion(String erpVersion) {
        this.erpVersion = erpVersion;
    }

    public String getEdiUploadPath() {
        //如果没有写Edi上传路径
        if (!haveCreateEdiPath) {
            File file = new File(ediUploadPath);
            file.mkdirs();
            haveCreateEdiPath = true;
        }
        return ediUploadPath;
    }

    public void setEdiUploadPath(String ediUploadPath) {
        this.ediUploadPath = ediUploadPath;
    }

    public Boolean getKaptchaOpen() {
        return kaptchaOpen;
    }

    public void setKaptchaOpen(Boolean kaptchaOpen) {
        this.kaptchaOpen = kaptchaOpen;
    }

    public Boolean getSwaggerOpen() {
        return swaggerOpen;
    }

    public void setSwaggerOpen(Boolean swaggerOpen) {
        this.swaggerOpen = swaggerOpen;
    }

    public Boolean getSpringSessionOpen() {
        return springSessionOpen;
    }

    public void setSpringSessionOpen(Boolean springSessionOpen) {
        this.springSessionOpen = springSessionOpen;
    }

    public Integer getSessionInvalidateTime() {
        return sessionInvalidateTime;
    }

    public void setSessionInvalidateTime(Integer sessionInvalidateTime) {
        this.sessionInvalidateTime = sessionInvalidateTime;
    }

    public Integer getSessionValidationInterval() {
        return sessionValidationInterval;
    }

    public void setSessionValidationInterval(Integer sessionValidationInterval) {
        this.sessionValidationInterval = sessionValidationInterval;
    }

    /**
     * ACCOUNT_SID:开发者主账号ID(注册后自动生成)
     * AUTH_TOKEN:开发者账号认证密匙(注册后自动生成)
     */
    private String ACCOUNT_SID;
    private String AUTH_TOKEN;

    public String getACCOUNT_SID() {
        return ACCOUNT_SID;
    }

    public void setACCOUNT_SID(String ACCOUNT_SID) {
        this.ACCOUNT_SID = ACCOUNT_SID;
    }

    public String getAUTH_TOKEN() {
        return AUTH_TOKEN;
    }

    public void setAUTH_TOKEN(String AUTH_TOKEN) {
        this.AUTH_TOKEN = AUTH_TOKEN;
    }


    private static E4jProperties OWN = null;
    public static E4jProperties getOWN(){
        if(OWN==null){
            OWN = SpringContextHolder.getBean(E4jProperties.class);
        }
        return OWN;
    }

}
